//
//  CaptureSelectView.m
//  Unity-iPhone
//
//  Created by Karl on 2017/3/29.
//
//

#import "CaptureSelectView.h"
#import "SelectLayout.h"
#import "UICollectionView+Scale.h"
#import "CaptureSelectViewCell.h"

@interface CaptureSelectView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) NSArray *items;
@property (nonatomic,strong) NSArray *actors;
@property (nonatomic,strong) UICollectionView *collectionView;

@end

@implementation CaptureSelectView{
    
}

- (instancetype) init{
    self = [super init];
    if (self) {
        
        _items = @[@"item_fox",@"item_prience"];
        _actors = @[@"fox",@"prince"];
        
        SelectLayout *layout = [[SelectLayout alloc] init];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        [_collectionView setBackgroundColor:[UIColor clearColor]];
        [_collectionView setDelegate:self];
        [_collectionView setDataSource:self];
        [_collectionView setClipsToBounds:NO];
        _collectionView.showsHorizontalScrollIndicator = NO;

        [_collectionView registerClass:[CaptureSelectViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self addSubview:_collectionView];
        
        [_collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        
    }
    return self;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _items.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CaptureSelectViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];
    cell.image = _items[indexPath.item];
    return cell;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"你选中了第 %ld 个item",(long)indexPath.row);
    
    NSString *actor = _actors[indexPath.item];
    
    [self.collectionView expandItemAtIndexPath:indexPath animated:YES];
    UnitySendMessage("_sceneManager", "LoadLiveARCharacter", actor.UTF8String);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}


- (void)layoutSubviews{
    [super layoutSubviews];
    
    _collectionView.left = 0;
    _collectionView.top = 0;
    _collectionView.width = self.width;
    _collectionView.height = self.height;
}

@end
